package net.oschina.bilbodai.common.beanutil.bean.creator;

import net.oschina.bilbodai.common.beanutil.bean.code.IPropertyCodeApplier;
import net.oschina.bilbodai.common.beanutil.bean.code.ItMapToItMapApplier;
import net.oschina.bilbodai.common.beanutil.bean.core.PropertyDumpInfo;
import net.oschina.bilbodai.common.beanutil.bean.core.PropertyInfo;
import net.oschina.bilbodai.common.beanutil.bean.type.NullType;
import net.oschina.bilbodai.common.beanutil.exception.PropertyDumpException;

/**
 * @author daimao  Date: 16/5/30 Time: 上午11:34
 * @version $Id$
 */
public enum ITMapToITMapCreator implements IPropertyCodeApplierCreator {
    INSTANCE;

    @Override
    public IPropertyCodeApplier create(PropertyDumpInfo dumpInfo, PropertyInfo sourceInfo, PropertyInfo targetInfo) {
        if (dumpInfo.getCollectTypeImpl().equals(NullType.class)) {
            throw new PropertyDumpException("CollectTypeImpl Type must be specified!");
        }
        if (targetInfo.getGetter() == null) {
            throw new PropertyDumpException("Using Map to dump field must contains Getter for null check!");
        }
        if (!dumpInfo.getTargetKeyType().isAssignableFrom(dumpInfo.getSrcKeyType())) {
            throw new PropertyDumpException("Using Map to dump field, source Map KeyType should assigned to target Map KeyType");
        }
        if (!dumpInfo.getTargetComponentType().isAssignableFrom(dumpInfo.getSrcComponentType())) {
            throw new PropertyDumpException("Using Map to dump field, source Map ValueType should assigned to target Map ValueType");
        }
        return new ItMapToItMapApplier(dumpInfo);
    }
}